package ch.fhnw.qualiscan.application.elements;

import java.util.Arrays;
import java.util.List;

import ch.fhnw.qualiscan.application.models.DomainModel;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.DomainText;
import ch.fhnw.qualiscan.core.helpers.SessionHelper;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
public class WeighDomain extends VerticalLayout implements Property.ValueChangeListener {
	
	public Domain domain;
	public DomainModel domainModel;
	
	private int weight = 2;
	
	public WeighDomain(Domain d) {
		domainModel = new DomainModel();
		this.domain = domainModel.getDomainById(d.getId());
		setWidth("100%");
		buildLayout();
	}
	
	public void buildLayout() {
		HorizontalLayout domainWrap = new HorizontalLayout();
		domainWrap.setWidth("800px");
		
		// load data
		SessionHelper sessionHelper = new SessionHelper();

		DomainText domainText = domainModel.getDomainText(domain, sessionHelper.getUserSession().getLanguage());
		
		// text
		Label domainTextLabel = new Label(domainText.getText());
		
		// optiongroup
		List<Integer> weights = Arrays.asList(new Integer[] {1,2,3});
		OptionGroup weightPicker = new OptionGroup(null, weights);
		
		weightPicker.addStyleName("h-optiongroup weightPicker");
		weightPicker.setItemCaption(1, "");
		weightPicker.setItemCaption(2, "");
		weightPicker.setItemCaption(3, "");
		weightPicker.setWidth("270px");
		weightPicker.setImmediate(true);
		weightPicker.select(2);
		weightPicker.addListener(this);
		
		HorizontalLayout weightPickerWrap = new HorizontalLayout();
		weightPickerWrap.addComponent(weightPicker);
		
		domainWrap.addComponent(domainTextLabel);
		domainWrap.addComponent(weightPickerWrap);
		domainWrap.setExpandRatio(domainTextLabel, 1.0f);
		
		addComponent(domainWrap);
	}

	public void valueChange(ValueChangeEvent event) {
		weight = (Integer) event.getProperty().getValue();
	}
	
	public int getWeight() {
		return weight;
	}
	
}
